package com.srwl.mytx.fragment;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.srwl.mytx.R;
import com.srwl.mytx.im.ConversationService;
import com.srwl.mytx.im.IMSClientBootstrap;
import com.srwl.mytx.widget.BottomBarBadge;
import com.srwl.mytx.widget.ConversationListView;
import com.srwl.mytx.widget.LocalSearchView;

/**
 * conversation list fragment
 */
public class BaseConversationListFragment extends BaseFragment {
    private static final String TAG = "BaseConversation";
    private final static int MSG_REFRESH = 2;
    protected EditText et_localSearch;
    protected ImageButton clearSearch;
    protected boolean hidden;
    protected ConversationListView conversationListView;
    protected boolean isConflict;
    protected View toolBar;
    protected BottomBarBadge unreadPushMsgNumber;
    protected TextView tv_unReadNoticeMsgNo;
    protected ConversationService conversationService;
    LocalSearchView localSearchView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_conversation_list_v1, container, false);
        initView(view);
        setUpView();
        return view;
    }


    protected void initView(View view) {
        //  inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);

        conversationListView = view.findViewById(R.id.list);
        et_localSearch = view.findViewById(R.id.et_local_search);
        clearSearch = view.findViewById(R.id.search_clear);
        toolBar = view.findViewById(R.id.rl_tool_bar);
        unreadPushMsgNumber = view.findViewById(R.id.unread_push_msg_number);
        tv_unReadNoticeMsgNo = view.findViewById(R.id.tv_unread_notice_msg_number);
        localSearchView = view.findViewById(R.id.local_search_view);
    }


    protected void setUpView() {

        et_localSearch.addTextChangedListener(new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    localSearchView.search(s.toString());
                    conversationListView.setVisibility(View.GONE);
                    localSearchView.setVisibility(View.VISIBLE);
                    clearSearch.setVisibility(View.VISIBLE);
                } else {
                    clearSearch.setVisibility(View.INVISIBLE);
                    localSearchView.setVisibility(View.GONE);
                    conversationListView.setVisibility(View.VISIBLE);
                }
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            public void afterTextChanged(Editable s) {
            }
        });
        clearSearch.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                et_localSearch.getText().clear();
                hideSoftKeyboard();
            }
        });


    }

    protected void initConversationListView() {
        conversationService = IMSClientBootstrap.getInstance().getConversationService();
        conversationListView.init(conversationService);
        conversationListView.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                hideSoftKeyboard();
                return false;
            }
        });

    }


    /**
     * 刷新对话列表
     */
    public void refresh() {
        if (conversationListView != null) {
            conversationListView.refresh();
        }
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        this.hidden = hidden;
//        if (!hidden && !isConflict) {
//            conversationListView.refresh();
//        }
    }

    @Override
    public void onResume() {
        super.onResume();
       // conversationListView.refresh();

    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (conversationService != null) {
            conversationService.removeConversationListener();
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (isConflict) {
            outState.putBoolean("isConflict", true);
        }
    }


}
